<template>
  <div class="login-container">
    <div class="login-box">
      <div class="login-header">
        <div class="logo">
          <i class="el-icon-monitor"></i>
        </div>
        <h2>服务器迁移管理系统</h2>
        <p>Server Migration Management System</p>
      </div>
      
      <el-form
        :model="loginForm"
        :rules="loginRules"
        ref="loginForm"
        class="login-form"
      >
        <el-form-item prop="username">
          <el-input 
            v-model="loginForm.username" 
            placeholder="请输入用户名"
            prefix-icon="el-icon-user"
            size="large"
            clearable
            @focus="handleInputFocus"
            @blur="handleInputBlur"
          >
          </el-input>
        </el-form-item>
        
        <el-form-item prop="password">
          <el-input
            v-model="loginForm.password"
            type="password"
            placeholder="请输入密码"
            prefix-icon="el-icon-lock"
            size="large"
            show-password
            @focus="handleInputFocus"
            @blur="handleInputBlur"
            @keyup.enter.native="handleLogin"
          >
          </el-input>
        </el-form-item>
        
        <el-form-item>
          <el-button
            type="primary"
            @click="handleLogin"
            :loading="loading"
            size="large"
            class="login-button"
            round
          >
            <i v-if="!loading" class="el-icon-right"></i>
            {{ loading ? '登录中...' : '登录' }}
          </el-button>
        </el-form-item>
      </el-form>
      
      <div class="login-footer">
        <p>© 2025 服务器迁移管理系统 - 专业的企业级迁移解决方案</p>
      </div>
    </div>
  </div>
</template>

<script>
import { mapActions } from 'vuex'

export default {
  name: 'Login',
  data() {
    return {
      loginForm: {
        username: '',
        password: ''
      },
      loginRules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 3, message: '密码长度不能少于3位', trigger: 'blur' }
        ]
      },
      loading: false
    }
  },
  methods: {
    ...mapActions('auth', ['login']),
    handleInputFocus(event) {
      event.target.parentElement.parentElement.classList.add('focused')
    },
    handleInputBlur(event) {
      event.target.parentElement.parentElement.classList.remove('focused')
    },
    async handleLogin() {
      this.$refs.loginForm.validate(async (valid) => {
        if (valid) {
          this.loading = true
          try {
            const response = await this.login(this.loginForm)
            this.$message.success('登录成功')
            this.$router.push('/')
          } catch (error) {
            this.$message.error(error.message || '登录失败')
          } finally {
            this.loading = false
          }
        } else {
          this.$message.error('请填写正确的登录信息')
        }
      })
    }
  }
}
</script>

<style scoped>
.login-container {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  background-size: 400% 400%;
  animation: gradientBG 15s ease infinite;
  position: relative;
  overflow: hidden;
}

.login-container::before {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: radial-gradient(circle at 10% 20%, rgba(255, 255, 255, 0.1) 0%, transparent 20%),
              radial-gradient(circle at 90% 80%, rgba(255, 255, 255, 0.1) 0%, transparent 20%);
  animation: floating 20s ease-in-out infinite;
}

@keyframes gradientBG {
  0% {
    background-position: 0% 50%;
  }
  50% {
    background-position: 100% 50%;
  }
  100% {
    background-position: 0% 50%;
  }
}

@keyframes floating {
  0% {
    transform: translateY(0px);
  }
  50% {
    transform: translateY(20px);
  }
  100% {
    transform: translateY(0px);
  }
}

.login-box {
  width: 450px;
  background: rgba(255, 255, 255, 0.95);
  border-radius: 20px;
  box-shadow: 0 25px 50px rgba(0, 0, 0, 0.3);
  padding: 40px 30px;
  backdrop-filter: blur(10px);
  transform: translateY(0);
  transition: all 0.3s ease;
  position: relative;
  z-index: 2;
}

.login-box:hover {
  transform: translateY(-10px);
  box-shadow: 0 30px 60px rgba(0, 0, 0, 0.4);
}

.login-header {
  text-align: center;
  margin-bottom: 40px;
}

.logo {
  width: 90px;
  height: 90px;
  margin: 0 auto 25px;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  color: white;
  font-size: 40px;
  box-shadow: 0 10px 25px rgba(0, 0, 0, 0.3);
  animation: pulse 2s infinite;
}

@keyframes pulse {
  0% {
    box-shadow: 0 0 0 0 rgba(102, 126, 234, 0.4);
  }
  70% {
    box-shadow: 0 0 0 15px rgba(102, 126, 234, 0);
  }
  100% {
    box-shadow: 0 0 0 0 rgba(102, 126, 234, 0);
  }
}

.login-header h2 {
  color: #333;
  font-size: 28px;
  font-weight: 700;
  margin: 0 0 12px 0;
  letter-spacing: 1px;
}

.login-header p {
  color: #666;
  font-size: 15px;
  margin: 0;
  font-weight: 300;
}

.login-form {
  margin-bottom: 35px;
}

.login-form ::v-deep .el-form-item {
  margin-bottom: 25px;
  position: relative;
}

.login-form ::v-deep .el-form-item.focused .el-input__inner {
  border-color: #667eea;
  box-shadow: 0 0 0 3px rgba(102, 126, 234, 0.2);
}

.login-form ::v-deep .el-input__inner {
  height: 55px;
  line-height: 55px;
  padding-left: 50px;
  border-radius: 12px;
  border: 2px solid #e1e5e9;
  transition: all 0.3s ease;
  font-size: 16px;
}

.login-form ::v-deep .el-input__inner:focus {
  border-color: #667eea;
  box-shadow: 0 0 0 3px rgba(102, 126, 234, 0.2);
}

.login-form ::v-deep .el-input__prefix {
  left: 18px;
  color: #909399;
}

.login-form ::v-deep .el-input__prefix i {
  font-size: 20px;
}

.login-button {
  width: 100%;
  height: 55px;
  font-size: 17px;
  font-weight: 600;
  letter-spacing: 2px;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  border: none;
  transition: all 0.3s ease;
  box-shadow: 0 5px 15px rgba(102, 126, 234, 0.4);
}

.login-button:hover {
  transform: translateY(-3px);
  box-shadow: 0 10px 25px rgba(102, 126, 234, 0.5);
}

.login-button:active {
  transform: translateY(0);
}

.login-footer {
  text-align: center;
  margin-top: 25px;
}

.login-footer p {
  color: #909399;
  font-size: 13px;
  margin: 0;
  font-weight: 300;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .login-box {
    width: 90%;
    margin: 0 15px;
    padding: 30px 20px;
  }
  
  .login-header h2 {
    font-size: 24px;
  }
  
  .login-form ::v-deep .el-input__inner {
    height: 50px;
    line-height: 50px;
  }
  
  .login-button {
    height: 50px;
    font-size: 16px;
  }
}
</style>